package com.hzqy.service.userActivityInfo;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import com.hzqy.persistence.dao.userActivityInfo.UserActivityInfoDao;
import com.hzqy.service.core.GenericServiceBean;
import com.hzqy.web.filter.LoginFilter;
import com.hzqy.web.vo.CmContentVo;
import com.hzqy.web.vo.UserActivityInfoVo;

@Service
public class UserActivityInfoServiceBean extends GenericServiceBean<UserActivityInfoVo,UserActivityInfoDao, Integer>{
	private static final long serialVersionUID = 1L;
	private static final Log LOG = LogFactory.getLog(LoginFilter.class);

	@Resource
	private UserActivityInfoDao userActivityInfoDao;
	@Override
	public UserActivityInfoDao getEntityDao() {
		return userActivityInfoDao;
	}
	
	public Map<String,Object> insertUserActivityInfo(UserActivityInfoVo userActivityInfoVo) {
		Map<String,Object> map = new HashMap<String,Object>();
		int flag = userActivityInfoDao.insertUserActivityInfo(userActivityInfoVo);
		if(flag>0) {
			map.put("success", true);
		}else {
			map.put("success", false);
		}
		return map;
	}
	
	public Map<String,Object> updateUserActivityInfo(UserActivityInfoVo userActivityInfoVo) {
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("f_uai_username",userActivityInfoVo.getF_uai_username());
		map.put("f_uai_picId",userActivityInfoVo.getF_uai_picId());
		map.put("f_uai_grade",userActivityInfoVo.getF_uai_grade());
		map.put("f_uai_medal",userActivityInfoVo.getF_uai_medal());
		map.put("f_uai_competite",userActivityInfoVo.getF_uai_competite());
		map.put("f_uai_delayed",userActivityInfoVo.getF_uai_delayed());
		map.put("f_uai_regrets",userActivityInfoVo.getF_uai_regrets());
		map.put("f_uai_replace",userActivityInfoVo.getF_uai_replace());
		map.put("f_uai_obligate1",userActivityInfoVo.getF_uai_obligate1());
		map.put("f_uai_obligate2",userActivityInfoVo.getF_uai_obligate2());
		map.put("f_uai_serverid",userActivityInfoVo.getF_uai_serverid());
		map.put("f_uai_activityid",userActivityInfoVo.getF_uai_activityid());
		int flag = userActivityInfoDao.updateUserActivityInfo(map);
		if(flag>0) {
			map.put("success", true);
		}else {
			map.put("success", false);
		}
		return map;
	}
	
	public UserActivityInfoVo selectUserActivityInfo(UserActivityInfoVo userActivityInfoVo) {
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("f_uai_serverid",userActivityInfoVo.getF_uai_serverid());
		map.put("f_uai_activityid",userActivityInfoVo.getF_uai_activityid());
		return userActivityInfoDao.selectUserActivityInfo(map);
	}
	
	/*
	 	更新订购用户每月赠送的参赛权
	 */
	public Map<String,Object> updateUserActivityInfoCompetite(UserActivityInfoVo userActivityInfoVo) {
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("f_uai_serverid",userActivityInfoVo.getF_uai_serverid());
		map.put("f_uai_activityid",userActivityInfoVo.getF_uai_activityid());
		UserActivityInfoVo uaiv = userActivityInfoDao.selectUserActivityInfo(map);
		int flag = 0;
		//判断当月是否赠送参赛权
		if(uaiv == null) {
			userActivityInfoVo.setF_uai_competite(10);
			flag = userActivityInfoDao.insertUserActivityInfo(userActivityInfoVo);
		}else {
			String cTime = uaiv.getF_uai_cdate();
	        SimpleDateFormat sDateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	        try {
	            Date cdate = sDateFormat.parse(cTime);
	            Calendar cal = Calendar.getInstance();
	            cal.setTime(new Date());
	            int currMonth = cal.get(Calendar.MONTH);
	            cal.setTime(cdate);
	            int cMonth = cal.get(Calendar.MONTH);
	            if(currMonth > cMonth) {
	            	map.put("updateMonthCompetite",1);
	            	userActivityInfoVo.setF_uai_competite(uaiv.getF_uai_competite()+10);
	            	flag = userActivityInfoDao.updateUserActivityInfo(map);
	            }
	        } catch(Exception px) {
	            px.printStackTrace();
	        }
		}
		UserActivityInfoVo userActivityInfo = userActivityInfoDao.selectUserActivityInfo(map);
		map.put("UserActivityInfoVo",userActivityInfo);
		return map;
	}
	
	public List<CmContentVo> selectContentList(String catalogIds,String f_cc_onlinedate){
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("catalogIds",catalogIds);
		map.put("f_cc_onlinedate",f_cc_onlinedate);
		return userActivityInfoDao.selectContentList(map);
	}
}
